﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.Basic.Model;

namespace EIA.SLClient.PlatManage.PMMgr
{
    public class DicItemListViewModel : SimpleMvvmToolkit.ViewModelBase<DicItemListViewModel>
	{
		public DicItemListViewModel()
		{
            this.RegisterToReceiveMessages<DicType>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.DIC_TYPE_SELECTED, EP_DicTypeSelected);
            this.RegisterToReceiveMessages<DicItem>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.DIC_ITEM_NEW_CANCEL, EP_NewCancel);
		}

        private void EP_DicTypeSelected(object sender, SimpleMvvmToolkit.NotificationEventArgs<DicType> e)
        {
            SelectedDicType = e.Data;
            RetrieveItems();
        }

        private void EP_NewCancel(object sender, SimpleMvvmToolkit.NotificationEventArgs<DicItem> e)
        {
            DicItemCollection.Remove(e.Data);
        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.DIC_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.DIC_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected PagedCollectionView _DicItemCollection;
        /// <summary>
        /// 注释
        /// </summary>
        public PagedCollectionView DicItemCollection
        {
            get { return _DicItemCollection; }
            set
            {
                _DicItemCollection = value;
                NotifyPropertyChanged(m => m.DicItemCollection);
            }
        }

        protected DicType _SelectedDicType;
        /// <summary>
        /// 注释
        /// </summary>
        public DicType SelectedDicType
        {
            get { return _SelectedDicType; }
            set
            {
                _SelectedDicType = value;
                NotifyPropertyChanged(m => m.SelectedDicType);
                RetrieveItemsCommand.RaiseCanExecuteChanged();
                InsertItemCommand.RaiseCanExecuteChanged();
            }
        }

        #endregion
        #region command

        #region RetrieveItemsCommand

        private SimpleMvvmToolkit.DelegateCommand _RetrieveItemsCommand = null;
        public SimpleMvvmToolkit.DelegateCommand RetrieveItemsCommand
        {
            get
            {
                if (_RetrieveItemsCommand == null)
                {
                    _RetrieveItemsCommand = new SimpleMvvmToolkit.DelegateCommand(RetrieveItems, CanExcuteRetrieveItems);
                } return _RetrieveItemsCommand;
            }
            set { _RetrieveItemsCommand = value; NotifyPropertyChanged(m => m.RetrieveItemsCommand); }
        }

        public void RetrieveItems()
        {
            if (SelectedDicType == null)
            {
                DicItemCollection = null;
                return;
            }
            EIA.Basic.BLL.ClientProxy.XDictProxy proxy = new Basic.BLL.ClientProxy.XDictProxy();
            Busy();
            proxy.List(SelectedDicType.DicTid, int.MaxValue, 1, (types) =>
            {
                Free();
                DicItemCollection = new PagedCollectionView(types);

            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("获取字典类型列表发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteRetrieveItems()
        {
            return SelectedDicType != null;
        }

        #endregion

        #region InsertItemCommand

        private SimpleMvvmToolkit.DelegateCommand _InsertItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand InsertItemCommand
        {
            get
            {
                if (_InsertItemCommand == null)
                {
                    _InsertItemCommand = new SimpleMvvmToolkit.DelegateCommand(InsertItem, CanExcuteInsertItem);
                } return _InsertItemCommand;
            }
            set { _InsertItemCommand = value; NotifyPropertyChanged(m => m.InsertItemCommand); }
        }

        public void InsertItem()
        {
            if (SelectedDicType == null) return;
            int maxiid = 0;
            foreach (DicItem i in DicItemCollection.SourceCollection)
            {
                if (i.DicIid > maxiid) maxiid = i.DicIid;
            }
            DicItem dicitem = new DicItem();
            dicitem.DicTid = SelectedDicType.DicTid;
            dicitem.DicIid = maxiid + 1;
            dicitem.DicIname = "新字典";

            List<DicItem> lst = new List<DicItem>();
            lst.Add(dicitem);
            lst.AddRange(DicItemCollection.SourceCollection as IList<DicItem>);
            DicItemCollection = new PagedCollectionView(lst);

            this.SendMessage<DicItem>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.DIC_ITEM_NEW, new SimpleMvvmToolkit.NotificationEventArgs<DicItem>("", dicitem));
        }

        public bool CanExcuteInsertItem()
        {
            return SelectedDicType != null;
        }

        #endregion

        #region UpdateItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.DicItem> _UpdateItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.DicItem> UpdateItemCommand
        {
            get
            {
                if (_UpdateItemCommand == null)
                {
                    _UpdateItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.DicItem>(UpdateItem, CanExcuteUpdateItem);
                } return _UpdateItemCommand;
            }
            set { _UpdateItemCommand = value; NotifyPropertyChanged(m => m.UpdateItemCommand); }
        }

        public void UpdateItem(EIA.Basic.Model.DicItem p)
        {
            this.SendMessage<DicItem>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.DIC_ITEM_UPDATE, new SimpleMvvmToolkit.NotificationEventArgs<DicItem>("", p));
        }

        public bool CanExcuteUpdateItem(EIA.Basic.Model.DicItem p)
        {
            return true;
        }

        #endregion

        #region DeleteItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.DicItem> _DeleteItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.DicItem> DeleteItemCommand
        {
            get
            {
                if (_DeleteItemCommand == null)
                {
                    _DeleteItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.Basic.Model.DicItem>(DeleteItem, CanExcuteDeleteItem);
                } return _DeleteItemCommand;
            }
            set { _DeleteItemCommand = value; NotifyPropertyChanged(m => m.DeleteItemCommand); }
        }

        public void DeleteItem(EIA.Basic.Model.DicItem p)
        {
            if (System.Windows.MessageBox.Show(string.Format("确认删除字典项 [{0}]", p.DicIname), "提示", System.Windows.MessageBoxButton.OKCancel) != System.Windows.MessageBoxResult.OK) return;

            EIA.Basic.BLL.ClientProxy.XDictProxy proxy = new EIA.Basic.BLL.ClientProxy.XDictProxy();
            Busy();
            proxy.Delete(p, (r) =>
            {
                Free();
                if (r)
                {
                    DicItemCollection.Remove(p);
                }
                else
                {
                    // 未删除
                    System.Windows.MessageBox.Show("字典删除失败！","提示", System.Windows.MessageBoxButton.OK);
                }

            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("字典删除发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteDeleteItem(EIA.Basic.Model.DicItem p)
        {
            return true;
        }

        #endregion

        #endregion
	}
}